//
//  HPPopView.m
//  HPZZTong
//
//  Created by panbin on 13-11-26.
//  Copyright (c) 2013年 Handpay. All rights reserved.
//

#import "HPPopView.h"
#import "HPPopTableCell.h"
#import "ZZTongDefine.h"
#import <QuartzCore/QuartzCore.h>

@interface HPPopView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIImageView *bgImageView;

@property (nonatomic, retain) UITableView *popTableView;



@end

@implementation HPPopView

@synthesize popTableDatas;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImage *bgImage = [UIImage imageNamed:@"pull_down_box"];
        UIImage *resizeBgImage = [bgImage resizableImageWithCapInsets:UIEdgeInsetsMake(bgImage.size.height/2 - 1, bgImage.size.width/2 - 1, bgImage.size.height/2, bgImage.size.width/2)];
        
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [_bgImageView setImage:resizeBgImage];
        [self addSubview:_bgImageView];
        
#if (ISJSBank) && (ISJSBank == 1)
        self.popTableDatas = @[@{KEY_IMG_NAME: @"cmbc",KEY_BANK_NAME: @"中国民生银行"},
                               @{KEY_IMG_NAME: @"abc", KEY_BANK_NAME: @"中国农业银行"},
                               @{KEY_IMG_NAME: @"jsb", KEY_BANK_NAME: @"晋商银行"}];
        
#else
        self.popTableDatas = @[@{KEY_IMG_NAME: @"cmbc",KEY_BANK_NAME: @"中国民生银行"},
                               @{KEY_IMG_NAME: @"abc", KEY_BANK_NAME: @"中国农业银行"}];
#endif
        _popTableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 10, frame.size.width - 10, frame.size.height - 15)];
        _popTableView.backgroundColor = [UIColor clearColor];
        _popTableView.delegate = self;
        _popTableView.dataSource = self;
        [self addSubview:_popTableView];
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.popTableDatas count];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"HPPopTableCell";
    
    HPPopTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[[HPPopTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    [cell fillWithObject:[self.popTableDatas objectAtIndex:indexPath.row]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ((_delegate) && ([_delegate respondsToSelector:@selector(popView:withSelectId:)])) {
        [_delegate popView:self withSelectId:indexPath.row];
    }
}

- (void)dealloc {
    self.popTableDatas = nil;
    HP_RELEASE(_popTableView);
    HP_RELEASE(_bgImageView);
    [super dealloc];
}

@end
